package com.gsdkjgpt4.sys.controller;

import com.gsdkjgpt4.common.vo.Result;
import com.gsdkjgpt4.sys.entity.AuthRequest;
import com.gsdkjgpt4.sys.entity.History;
import com.gsdkjgpt4.sys.entity.User;
import com.gsdkjgpt4.sys.service.IHistoryService;
import com.gsdkjgpt4.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rendong
 * @since 2024-01-18
 */
@RestController
@RequestMapping("/history")
@CrossOrigin
public class HistoryController {
    @Autowired
    private IHistoryService historyService;

    //通过userId搜索历史记录
    @PostMapping("/selectByUserId")
    public Result<?> selectByUserId(@RequestBody User user){
        Map<String,Object> data = historyService.selectHistoryByUserId(user.getUserId());
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(0,"历史记录检索失败");
    }

    //（测试用）返回gpt消息
    @PostMapping("/send")
    public Result<?> send(@RequestBody String msg){
        Map<String,Object> data = historyService.send();
        return Result.success(data);
    }

    //生成新的历史记录
    @PostMapping("/generateHistory")
    public Result<?> generateHistory(@RequestBody History history){
        Map<String,Object> data = historyService.generateHistory(history.getIsChatgpt(),history.getContext(),history.getUserId());
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(0,"历史记录创建失败");
    }
}
